import Vue from 'vue'
import VueRouter from 'vue-router'
// 在这里导入组件，当项目运行的时候，就会全部导入
import MyLogin from '@/components/MyLogin.vue';
import { Ls } from 'dayjs';
// import { from } from 'core-js/core/array';

Vue.use(VueRouter)



// 一级路由，对应的组件会展示到App中，所以占位符放在 App.vue 中
const routes = [
  //重定向,指定开始跳转，当访问 /转到 /home
  { path: '/', redirect: '/home' },
  // 一级路由:登录组件
  { path: '/login', component: MyLogin },
  // 一级路由:首页组件
  {
    path: '/home',
    component: () => import('@/components/MyHome.vue'),
    children: [
      //二级路由,哈希地址 /需要省略,方便后续拼接
      { path: "users", component: () => import('@/components/menus/MyUsers.vue') },
      { path: "orders", component: () => import('@/components/menus/MyOrders.vue') },
      { path: "rights", component: () => import('@/components/menus/MyRights.vue') },
      { path: "settings", component: () => import('@/components/menus/MySettings.vue') },
      { path: "goods", component: () => import('@/components/menus/MyGoods.vue') },
      //传参的方式实现
      { path: 'detail/:id', component: () => import('@/components/user/MyUserDetail.vue') }

    ]
  },
  { path: '*', component: () => import('@/components/NotFound.vue') },
]
const router = new VueRouter({
  mode: 'history',
  routes
})
//添加全局前置导航
router.beforeEach((to, from, next) => {
  if (to.path === 'login') {
    next()//访问登录页，直接放行
  } else {
    if (localStorage.getItem('token ')) {
      next()//本地存储token,说明登录过,(直接放行)
    } else {

      next('/login')//
    }
  }
})
// 路由导航练习，暂时注释
// router.beforeEach((to, from, next) => {
//   // console.log(to.path)
//   if (to.path === '/login') {
//     next()
//   }else{
//     if(localStorage.setItem('token')){
//       next()  
//     }else{
//       next('/login')
//     }
//   }

// })


export default router
